﻿using Common.Contract.Customer.Identifier;
using DataCenter.Configuration;
using DataCenter.Data;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Threading.Tasks;
using Common.Type;

namespace DataCenter.Provider
{
    public class ProvinceProvider : GenericProvider, IProvinceProvider
    {
        public List<Province> SearchProvinces(out int totalRecord, string textSearch, int pagesize = 0, int pageindex = 0)
        {
            var searcher = (textSearch ?? string.Empty).ToUpper();

            var query = repository.GetQuery<Province>().Include(x => x.Region);
            if (Identifier.IsValid(searcher))
            {
                ProvinceIdentifier identifier = new ProvinceIdentifier(searcher);
                int id = identifier.PersistableID;
                query = query.Where(x => x.Id == id);
            }
            else
                query = query.Where(x => x.Name.ToUpper().Contains(searcher));

            query = query.OrderBy(x => x.Id);
            totalRecord = query.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }

        public List<Province> SearchProvinces(out int totalRecord, string provinceIdentifier, string provinceName,
            decimal? lng, decimal? lat, string regionIdentifier,
            int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<Province>().Include(x => x.Region);
            if (ProvinceIdentifier.IsValid(provinceIdentifier))
            {
                ProvinceIdentifier identifier = new ProvinceIdentifier(provinceIdentifier);
                int id = identifier.PersistableID;
                querry = querry.Where(x => x.Id == id);
            }

            if (!string.IsNullOrWhiteSpace(provinceName))
                querry = querry.Where(x => x.Name.ToUpper().Contains(provinceName.ToUpper()));

            if (lng.HasValue)
                querry = querry.Where(x => x.Lng == lng);

            if (lng.HasValue)
                querry = querry.Where(x => x.Lat == lat);

            if (!string.IsNullOrWhiteSpace(regionIdentifier) && regionIdentifier != SystemConfiguration.ALL_REGION_CODE)
                querry = querry.Where(x => x.RegionId == regionIdentifier);

            querry = querry.OrderBy(x => x.Id);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public Province RetrieveProvince(string provinceIdentifier)
        {
            if (ProvinceIdentifier.IsValid(provinceIdentifier))
            {
                ProvinceIdentifier identifier = new ProvinceIdentifier(provinceIdentifier);
                int id = identifier.PersistableID;
                var querry = repository.GetQuery<Province>(x => x.Id == id).Include(x => x.Region);
                return querry.FirstOrDefault();
            }
            return null;
        }

        public bool Add(Province record)
        {
            try
            {
                repository.Provinces.Add(record);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch (Exception exp)
            {
                return false;
            }
        }

        public bool Update(Province record)
        {
            try
            {
                var updateRecord = repository.FindOne<Province>(x => x.Id == record.Id);
                if (updateRecord == null) return false;
                updateRecord.Name = record.Name;
                updateRecord.EntryUserId = record.EntryUserId;
                updateRecord.EntryUserName = record.EntryUserName;
                updateRecord.Lat = record.Lat;
                updateRecord.Lng = record.Lng;
                updateRecord.RegionId = record.RegionId;
                updateRecord.UpdateUserId = record.UpdateUserId;
                updateRecord.UpdateUserName = record.UpdateUserName;
                updateRecord.UpdateDateTime = DateTime.Now;
                updateRecord.SoTransferDate = record.SoTransferDate;
                updateRecord.SoTransferFlag = record.SoTransferFlag;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch (Exception exp)
            {
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                repository.Delete<Province>(x => x.Id == id);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch (Exception exp)
            {
                return false;
            }
        }

    }
}
